package com.sanhe.a448.activity;

import android.content.Intent;

import com.google.gson.Gson;
import com.menu.app.activity.BaseCustomInfoActivity;
import com.menu.app.adapter.BaseCusRecordAdapter;
import com.menu.app.bean.DayRecord;
import com.menu.app.db.bean.CusUsage;
import com.menu.app.db.bean.Custom;
import com.menu.app.db.bean.Doc;
import com.menu.app.fragment.BaseDialogFragment;
import com.menu.app.util.Utils;
import com.sanhe.a448.App;
import com.sanhe.a448.activity.work.ModeActivity;
import com.sanhe.a448.activity.work.WorkActivity;
import com.sanhe.a448.adapter.CusRecordAdapter;
import com.sanhe.a448.bean.WorkParam;
import com.sanhe.a448.event.Event;
import com.sanhe.a448.frag.ParamDlg;
import com.sanhe.a448.utils.Const;
import com.sanhe.a448.utils.PortPipe;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class CustomInfoActivity extends BaseCustomInfoActivity {
    @Override
    protected BaseCusRecordAdapter getAdapter() {
        return new CusRecordAdapter(this, dao, custom, binding.list, isAdmin);
    }

    @Override
    protected String createPdf(Doc doc, Custom custom, List<DayRecord> list) {
        return "";
    }

    @Override
    protected boolean supportPdf() {
        return false;
    }

    @Override
    protected List<DayRecord> combine(List<List<CusUsage>> l, Map<String, Integer> map) {
        List<DayRecord> list = new LinkedList<>();
        for (String key : map.keySet()) {
            DayRecord r = new DayRecord();
            r.setDay(key);
            r.record = l.get(map.get(key));
            list.add(r);
        }
        Iterator<DayRecord> it = list.iterator();
        while (it.hasNext()) {
            DayRecord r = it.next();
            if (r.img.isEmpty() && (r.record == null || r.record.isEmpty())) {
                it.remove();
            }
        }
        Collections.sort(list, (o1, o2) -> {
            long t1 = date2Ms(o1.getDay(0));
            long t2 = date2Ms(o2.getDay(0));
            if (t1 < t2) {
                return 1;
            } else if (t1 == t2) {
                return 0;
            }
            return -1;
        });
        return list;
    }

    @Override
    public void showReport(DayRecord record) {

    }

    ParamDlg dlg = null;

    @Override
    public void showParam(List<CusUsage> item) {
        dlg = new ParamDlg();
        dlg.setParam(item, custom, isAdmin);
        dlg.setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
            @Override
            public void onDismiss(boolean isOk, Object value) {
                if (!dlg.getDelete().isEmpty()) {
                    for (Long id : dlg.getDelete()) {
                        dao.delete(id);
                    }
                    getData();
                }
                if (isOk && value instanceof CusUsage) {
                    toWork((CusUsage) value);
                }
                dlg = null;
            }
        });
        dlg.show(getSupportFragmentManager(), "param");
    }

    @Override
    protected boolean checkHandle(Runnable runnable) {
        return true;
    }

    @Override
    protected boolean isStarted() {
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        App.getApp().setIgnore(true);
    }

    @Override
    public void startWork(CusUsage data) {
        PortPipe.getInstance().goBack(new Runnable() {
            @Override
            public void run() {
                App.getApp().setIgnore(false);
                Gson g = Utils.getGson();
                WorkParam param = g.fromJson(g.toJson(data.info), WorkParam.class);
                ModeActivity.mode = param.workType;
                App.getApp().setWorkParam(param);
                switch (param.workType) {
                    case 1:
                        Const.sendTouchEvent(0x83, 0x10, 0xb, 1, 0xb);
                        break;
                    case 2:
                        Const.sendTouchEvent(0x83, 0x10, 0xb, 1, 0xa);
                        break;
                    case 3:
                        Const.sendTouchEvent(0x83, 0x10, 0xb, 1, 0xc);
                        break;
                    case 4:
                        Const.sendTouchEvent(0x83, 0x10, 0xb, 1, 0xd);
                        break;
                    default:
                        return;
                }
                jump(param);
            }
        });
    }

    private void jump(WorkParam param) {
        if (!Const.jump) {
            return;
        }
        switch (param.workType) {
            case 1:
                Event.mode.postValue(2);
                break;
            case 2:
                Event.mode.postValue(1);
                break;
            case 4:
                Event.mode.postValue(4);
                break;
            case 3:
                Event.mode.postValue(3);
                break;
        }
        Event.stateSurface.postValue(4);
        Event.stateDeep.postValue(4);
        Event.start.postValue(4);
        Intent i = new Intent(this, WorkActivity.class);
        startActivity(i);
    }
}
